package com.ruoyi.jeeplus.modules.screen.frontdesk.screen_PV.service.impl;

import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_PV.domain.Frontdesk_screen_PV;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_PV.mapper.FrontdeskScreenPVMapper;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_PV.service.IFrontdesks_screenPVService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Service
public class Frontdesk_screenServiceImpl implements IFrontdesks_screenPVService {
    @Autowired
    FrontdeskScreenPVMapper frontdeskScreenPVMapper;

    //这个不用
    public List<Frontdesk_screen_PV> selectScreenPVList() {
        List<Frontdesk_screen_PV> frontdesk_screen_pvList = frontdeskScreenPVMapper.selectScreenPVList();
        List<Frontdesk_screen_PV> newPV = frontdesk_screen_pvList.stream().filter(Frontdesk_screen_PV -> {
            return Frontdesk_screen_PV.getAreaId().equals("110100");
        }).collect(Collectors.toList());
        return newPV;

        //return frontdeskScreenPVMapper.selectScreenPVList();
    }

    //刘奥旸算法开始咯：
    //递归查找所有分类的分类
    private List<Frontdesk_screen_PV> getChildrenCategories
    (Frontdesk_screen_PV root, List<Frontdesk_screen_PV> all) {
        List<Frontdesk_screen_PV> children = all.stream().filter(categoryEntity -> {
            //寻找其子类，必须用equals
            return categoryEntity.getAreaParentId().equals(root.getAreaId());
        }).map(categoryEntity -> {
//1、找到子菜单, 并设置

            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
            return categoryEntity;
        }).collect(Collectors.toList());
        return children;
    }
    //根据子类的电量计算出父类的光伏数据

    //这个是使用的
    public List<Frontdesk_screen_PV> exer(String id, String date_string) {
//        List<Frontdesk_screen_PV> frontdesk_screen_pvList = frontdeskScreenPVMapper.selectScreenPVList();
        int date_int = Integer.parseInt(date_string);
        String date = getTodayDate(date_int);
        date = "2024-10-27%";
        List<Frontdesk_screen_PV> frontdesk_screen_pvList = frontdeskScreenPVMapper.selectScreenPVList_bydate(date);
        List<Frontdesk_screen_PV> newPV = frontdesk_screen_pvList.stream().filter(Frontdesk_screen_PV -> {
            //这是主类id
            return Frontdesk_screen_PV.getAreaId().equals(id);
        }).map(
                category -> {
                    category.setChildrenCategories(getChildrenCategories(category, frontdesk_screen_pvList));
                    return category;
                }
        ).collect(Collectors.toList());
        calPV(newPV); // 根据子类的 PV 值计算出父类的 PV 值
//return frontdesk_screen_pvList;
        return newPV; // 返回包含层级关系和 PV 值计算结果的分类列表
    }


    //这个是可以用的,二开使用
    public List<Frontdesk_screen_PV> layexer(String id, String date) {
//        List<Frontdesk_screen_PV> frontdesk_screen_pvList = frontdeskScreenPVMapper.selectScreenPVList();
//        int date_int = Integer.parseInt(date_string);
//        String date = getTodayDate(date_int);
//        date = date + "%";
        List<Frontdesk_screen_PV> frontdesk_screen_pvList = frontdeskScreenPVMapper.selectScreenPVList_bydate(date);
        List<Frontdesk_screen_PV> newPV = frontdesk_screen_pvList.stream().filter(Frontdesk_screen_PV -> {
            //这是主类id
            return Frontdesk_screen_PV.getAreaId().equals(id);
        }).map(
                category -> {
                    category.setChildrenCategories(getChildrenCategories(category, frontdesk_screen_pvList));
                    return category;
                }
        ).collect(Collectors.toList());
        calPV(newPV); // 根据子类的 PV 值计算出父类的 PV 值
//return frontdesk_screen_pvList;
       return newPV; // 返回包含层级关系和 PV 值计算结果的分类列表
    }

    // 根据子类的光伏效果计算出父类的光伏
    private void calPV(List<Frontdesk_screen_PV> all) {
        // 遍历所有分类
        for (Frontdesk_screen_PV node : all) {
            // 检查当前节点是否有子类
            if (node.getChildrenCategories() != null && !node.getChildrenCategories().isEmpty()) {
                // 递归调用，计算子类的 PV 值
                calPV(node.getChildrenCategories());
                // 计算子类的总 PV 值
                BigDecimal totalPvGeneration = node.getChildrenCategories().stream()
                        .map(Frontdesk_screen_PV::getPvGeneration) // 提取每个子类的 PV 值
                        .filter(Objects::nonNull) // 过滤掉 null 值，避免计算错误
                        .reduce(BigDecimal.ZERO, BigDecimal::add); // 计算总和，初始值为 0
                // 将子类总 PV 加到当前节点的 PV 中
                node.setPvGeneration(totalPvGeneration.add(node.getPvGeneration() != null ? node.getPvGeneration() : BigDecimal.ZERO));
            }
        }
    }

    //获取日期
    @Override
    public String getTodayDate(int id) {
        LocalDate today = LocalDate.now();
        String result;
        switch (id) {
            case 0:
                //完成拼接数据查询
                result = "";
                break;
            case 1:
                result = String.valueOf(today.getYear()); // 返回年
                result = result;
                break;
            case 2:
                result = today.format(DateTimeFormatter.ofPattern("yyyy-MM")); // 返回年月
                result = result;
                break;
            case 3:
                result = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 返回年月日
                result = result;
                break;
            case 9:
                // 获取上个月的日期
                LocalDate lastMonthDate = today.minusMonths(1);
                // 格式化为 YYYY-MM
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
                String formattedDate = lastMonthDate.format(formatter);
                return formattedDate;
            case 10:
                LocalDate yesterday = LocalDate.now().minusDays(1);
                result = yesterday + ""; // 返回年月日
                result = result;
                break;
            default:
                result = "无效的格式"; // 处理无效的参数
        }
        // System.out.println("result==" + result);
        return result;
    }

    //这是辅助方法，循环查询

    //这是最新的方法，进行了功能增加
    public List<BigDecimal> laynew(String id, int date) {
        ArrayList<BigDecimal> countlist = new ArrayList<>();

        //String todayDate = getTodayDate(3);
        BigDecimal pvGeneration = new BigDecimal(0);
        if (date == 4) {
            String currentDate = getTodayDate(3); // 假设这个方法返回类似 "2024-10-24" 的字符串
            //

            for (int i = 8; i <= 19; i++) {
                //todayDate = todayDate + " " + i;
                BigDecimal count = new BigDecimal(0);
                String todayDate = String.format("%s %02d", currentDate, i);
                todayDate = todayDate + "%";
                //System.out.println(todayDate);
                List<Frontdesk_screen_PV> layexer = layexer(id, todayDate);
                //System.out.println(i + "()" + layexer);
                if (!layexer.isEmpty()) {
                    pvGeneration = layexer.get(0).getPvGeneration();
                    countlist.add(pvGeneration);
                    // 处理 pvGeneration\
                }else {
                    countlist.add(count);
                }
            }
            //全部统计
            //System.out.println(countlist);
            //创建返回数组
            List<BigDecimal> summedData = new ArrayList<>();
            // 遍历原始数据，两个两个相加
            for (int i = 0; i < countlist.size(); i += 2) {
                if (i + 1 < countlist.size()) {
                    // 计算两个元素的和
                    summedData.add(countlist.get(i).add(countlist.get(i + 1)));
                } else {
                    // 如果是最后一个元素且没有配对，则单独添加
                    summedData.add(countlist.get(i));
                }
            }

            // 输出结果
            //System.out.println(summedData);

        return summedData;
    } else if (date == 10) {
//            ArrayList<BigDecimal> countlist = new ArrayList<>();
//            //String todayDate = getTodayDate(3);
//            BigDecimal pvGeneration = new BigDecimal(0);
            String currentDate = getTodayDate(10); // 假设这个方法返回类似 "2024-10-24" 的字符串
            //
            for (int i = 8; i <= 19; i++) {
                //todayDate = todayDate + " " + i;
                BigDecimal count = new BigDecimal(0);
                String todayDate = String.format("%s %02d", currentDate, i);
                todayDate = todayDate + "%";
                System.out.println(todayDate);
                List<Frontdesk_screen_PV> layexer = layexer(id, todayDate);
                System.out.println(i + "()" + layexer);
                if (!layexer.isEmpty()) {
                    pvGeneration = layexer.get(0).getPvGeneration();
                    countlist.add(pvGeneration);
                    // 处理 pvGeneration\
                }else {
                    countlist.add(count);
                }
            }
            //全部统计
            //System.out.println(countlist);
            //创建返回数组
            List<BigDecimal> summedData = new ArrayList<>();
            // 遍历原始数据，两个两个相加
            for (int i = 0; i < countlist.size(); i += 2) {
                if (i + 1 < countlist.size()) {
                    // 计算两个元素的和
                    summedData.add(countlist.get(i).add(countlist.get(i + 1)));
                } else {
                    // 如果是最后一个元素且没有配对，则单独添加
                    summedData.add(countlist.get(i));
                }
            }
            // 输出结果
            //System.out.println(summedData);
            return summedData;
        } else if (date == 9) {
            String currentDate = getTodayDate(9); // 假设这个方法返回类似 "2024-10-24" 的字符串
            int year = Integer.parseInt(currentDate.substring(0, 4)); // 提取年份
            int month = Integer.parseInt(currentDate.substring(5, 7)); // 提取月份
            for (int day = 1; day <= 30; day++) {
                BigDecimal count = new BigDecimal(0);
                // 假设最多30天
                String formattedDate = String.format("%04d-%02d-%02d", year, month, day);
                formattedDate = formattedDate + "%";
                // System.out.println(formattedDate); // 输出格式为 "2024-10-01", "2024-10-02", ...
                List<Frontdesk_screen_PV> layexer = layexer(id, formattedDate);
                //  System.out.println(day + "()" + layexer);
                if (!layexer.isEmpty()) {
                    pvGeneration = layexer.get(0).getPvGeneration();
                    countlist.add(pvGeneration);
                    // 处理 pvGeneration\
                } else {
                    countlist.add(count);
                }

                // System.out.println(currentDate);
            }

            //全部统计
            //System.out.println(countlist);
            //创建返回数组
            List<BigDecimal> summedData = new ArrayList<>();
            // 遍历原始数据，两个两个相加
            for (int i = 0; i < countlist.size(); i += 5) {
                BigDecimal sum = BigDecimal.ZERO; // 初始化总和
                for (int j = 0; j < 5; j++) { // 处理5个元素
                    if (i + j < countlist.size()) { // 确保索引不越界
                        sum = sum.add(countlist.get(i + j)); // 累加
                    }
                }
                summedData.add(sum); // 将每组的和添加到 summedData
            }
            //System.out.println(summedData);

            //System.out.println(summedData);
            return summedData;
        }else if (date == 2) {
            String currentDate = getTodayDate(2); // 假设这个方法返回类似 "2024-10-24" 的字符串
            int year = Integer.parseInt(currentDate.substring(0, 4)); // 提取年份
            int month = Integer.parseInt(currentDate.substring(5, 7)); // 提取月份
            for (int day = 1; day <= 30; day++) {
                BigDecimal count = new BigDecimal(0);
                // 假设最多30天
                String formattedDate = String.format("%04d-%02d-%02d", year, month, day);
                formattedDate = formattedDate + "%";
                // System.out.println(formattedDate); // 输出格式为 "2024-10-01", "2024-10-02", ...
                List<Frontdesk_screen_PV> layexer = layexer(id, formattedDate);
                //  System.out.println(day + "()" + layexer);
                if (!layexer.isEmpty()) {
                    pvGeneration = layexer.get(0).getPvGeneration();
                    countlist.add(pvGeneration);
                    // 处理 pvGeneration\
                } else {
                    countlist.add(count);
                }

                // System.out.println(currentDate);
            }

            //全部统计
            //System.out.println(countlist);
            //创建返回数组
            List<BigDecimal> summedData = new ArrayList<>();
            // 遍历原始数据，两个两个相加
            for (int i = 0; i < countlist.size(); i += 5) {
                BigDecimal sum = BigDecimal.ZERO; // 初始化总和
                for (int j = 0; j < 5; j++) { // 处理5个元素
                    if (i + j < countlist.size()) { // 确保索引不越界
                        sum = sum.add(countlist.get(i + j)); // 累加
                    }
                }
                summedData.add(sum); // 将每组的和添加到 summedData
            }
            System.out.println(summedData);

            System.out.println(summedData);
            return summedData;
        }
        else{
        //123没开发
        String todayDate = getTodayDate(date);
        todayDate = todayDate + "%";
        List<Frontdesk_screen_PV> layexer = layexer(id, todayDate);
//
        return null;
    }
}



}


